# -*- coding: utf-8 -*-
from datetime import datetime

from odoo import models, fields, api
from odoo.exceptions import UserError


class hr_contract_inherit(models.Model):
    _inherit = 'hr.contract'
    employee_id=fields.Many2one('hr.employee',required=True,string=u'员工')
    @api.onchange('employee_id')
    def onchange_employee_id_for_name(self):
        if self.employee_id.barcode:
            self.name = self.employee_id.barcode + '-' + datetime.today().strftime('%Y%m')


    _sql_constraints = [
        ('name_uniq', 'unique (name)', "合同参考已经存在"),
    ]
    name = fields.Char('Contract Reference', required=False)
    # @api.model
    # def create(self, vals_list):
    #     res=super(hr_contract_inherit, self).create(vals_list)
    #     if not res.name:
    #         if not res.employee_id.barcode:
    #             raise UserError('员工工号不存在请先设置')
    #         res.name=res.employee_id.barcode+'-'+datetime.today().strftime('%Y%M')
    #     return res



